package com.icarus.entity;

import com.icarus.view.MainView;

import javax.swing.*;

/**
 * 子弹
 */
public class Bullet implements Runnable{


    //子弹的坐标
    private int x;
    private int y;

    //子弹方向 0向上 1 向下
    private int direction;

    //子弹速度
    private int speed;

    //子弹状态默认为0
    //0 子弹还存在
    //1 子弹不存在
    private int status;
    //子弹图片
    private ImageIcon icon;


    public void setDirection(int direction) {
        this.direction = direction;
    }

    public int getDirection() {
        return direction;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    @Override
    public void run() {
        //子弹自己飞
        while (true) {
            try {
                //判断子弹是否还存在
                //飞出屏幕外则不存在了 更改状态为1
                if(this.status==0) {

                    //子弹向上飞
                    if(this.direction==0) {
                        this.y = this.y - this.speed;
                        if(this.y <= (0-this.icon.getIconHeight())) {
                            this.status = 1;
                        }
                    } else if(this.direction==1){
                        //子弹向下飞
                        this.y = this.y + this.speed;
                        if(this.y>= MainView.HEIGHT+this.icon.getIconHeight()) {
                            this.status = 1;
                        }
                    }

                } else {
                    break;  //跳出死循环 结束整个线程
                }
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
